<?php
defined('CR_BASE') or exit('hacking attempt');

//php底层代码已经定义了pdo类所以不能再用pdo定义类名
class DPdo extends Db{
    private static $_link=null;
    private $_res=null;
                    
    public function _initialize($params=array()){
        $this->connection();
    }
    
    protected function connection(){
        if (is_null($this->_link)){
                try {
                    if (!extension_loaded('pdo'))throw new Exception('服务器没有安装pdo扩展');
                    //dns格式为,驱动类型:参数1=参数值1;参数2=参数值2...,如 mysql:host=localhost;dbname=test
                    if (in_array(DB_DRIVER, array('mysql','mssql','sybase')))
                        $dns=DB_DRIVER.':host='.DB_HOST.';dbname='.DB_NAME;
                    else 
                        throw new Exception('该框架还不支持其他类型');//TODO
                    $charset=CHARSET;
                    strpos(strtolower(CHARSET), 'utf-8')!==false && $charset='utf8';
                    $opts=array(
                        PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION,
                        PDO::MYSQL_ATTR_INIT_COMMAND=>'set character_set_connection='.$charset.
                                                      ',character_set_results='.$charset.
                                                      ',character_set_client=binary'  
                    );
                    $this->_link=new PDO($dns, DB_USER, DB_PWD,$opts);
                }catch (PDOException $e){
                    echo '连接失败：'.$e->getMessage();
                }
        }
    }
    
    public function query($sql,$values){
        $stmt=$this->_link->prepare($sql);
        if ($res=$stmt->execute($values))
            $this->_res=$res;
        
        
    }
    
    public function getLastInsertId(){
        return $this->_link->lastInsertId();
    }




}